package bpmn.diagram.edit.parts;

import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import bpmn.diagram.part.Bpmn_1_1VisualIDRegistry;

/**
 * @generated
 */
public class Bpmn_1_1EditPartFactory implements EditPartFactory {

	/**
	 * @generated
	 */
	public EditPart createEditPart(EditPart context, Object model) {
		if (model instanceof View) {
			View view = (View) model;
			switch (Bpmn_1_1VisualIDRegistry.getVisualID(view)) {

			case ProcessEditPart.VISUAL_ID:
				return new ProcessEditPart(view);

			case PoolEditPart.VISUAL_ID:
				return new PoolEditPart(view);

			case WrappingLabelEditPart.VISUAL_ID:
				return new WrappingLabelEditPart(view);

			case Process2EditPart.VISUAL_ID:
				return new Process2EditPart(view);

			case ProcessNameEditPart.VISUAL_ID:
				return new ProcessNameEditPart(view);

			case TaskEditPart.VISUAL_ID:
				return new TaskEditPart(view);

			case TaskNameEditPart.VISUAL_ID:
				return new TaskNameEditPart(view);

			case Task2EditPart.VISUAL_ID:
				return new Task2EditPart(view);

			case TaskName2EditPart.VISUAL_ID:
				return new TaskName2EditPart(view);

			case Task3EditPart.VISUAL_ID:
				return new Task3EditPart(view);

			case TaskName3EditPart.VISUAL_ID:
				return new TaskName3EditPart(view);

			case Task4EditPart.VISUAL_ID:
				return new Task4EditPart(view);

			case TaskName4EditPart.VISUAL_ID:
				return new TaskName4EditPart(view);

			case Task5EditPart.VISUAL_ID:
				return new Task5EditPart(view);

			case TaskName5EditPart.VISUAL_ID:
				return new TaskName5EditPart(view);

			case Task6EditPart.VISUAL_ID:
				return new Task6EditPart(view);

			case TaskName6EditPart.VISUAL_ID:
				return new TaskName6EditPart(view);

			case Task7EditPart.VISUAL_ID:
				return new Task7EditPart(view);

			case TaskName7EditPart.VISUAL_ID:
				return new TaskName7EditPart(view);

			case Task8EditPart.VISUAL_ID:
				return new Task8EditPart(view);

			case TaskName8EditPart.VISUAL_ID:
				return new TaskName8EditPart(view);

			case Task9EditPart.VISUAL_ID:
				return new Task9EditPart(view);

			case TaskName9EditPart.VISUAL_ID:
				return new TaskName9EditPart(view);

			case StartEditPart.VISUAL_ID:
				return new StartEditPart(view);

			case IntermediateEditPart.VISUAL_ID:
				return new IntermediateEditPart(view);

			case EndEditPart.VISUAL_ID:
				return new EndEditPart(view);

			case DataObjectEditPart.VISUAL_ID:
				return new DataObjectEditPart(view);

			case DataObjectNameEditPart.VISUAL_ID:
				return new DataObjectNameEditPart(view);

			case InclusiveEditPart.VISUAL_ID:
				return new InclusiveEditPart(view);

			case ComplexEditPart.VISUAL_ID:
				return new ComplexEditPart(view);

			case ParallelEditPart.VISUAL_ID:
				return new ParallelEditPart(view);

			case DataBasedEditPart.VISUAL_ID:
				return new DataBasedEditPart(view);

			case LaneEditPart.VISUAL_ID:
				return new LaneEditPart(view);

			case LaneNameEditPart.VISUAL_ID:
				return new LaneNameEditPart(view);

			case Lane2EditPart.VISUAL_ID:
				return new Lane2EditPart(view);

			case LaneName2EditPart.VISUAL_ID:
				return new LaneName2EditPart(view);

			case PoolCompartmentPoolEditPart.VISUAL_ID:
				return new PoolCompartmentPoolEditPart(view);

			case ProcessCompartmentProcessEditPart.VISUAL_ID:
				return new ProcessCompartmentProcessEditPart(view);

			case LaneCompartmentLaneEditPart.VISUAL_ID:
				return new LaneCompartmentLaneEditPart(view);

			case SequenceFlowEditPart.VISUAL_ID:
				return new SequenceFlowEditPart(view);

			case AssociationEditPart.VISUAL_ID:
				return new AssociationEditPart(view);

			}
		}
		return createUnrecognizedEditPart(context, model);
	}

	/**
	 * @generated
	 */
	private EditPart createUnrecognizedEditPart(EditPart context, Object model) {
		// Handle creation of unrecognized child node EditParts here
		return null;
	}

	/**
	 * @generated
	 */
	public static CellEditorLocator getTextCellEditorLocator(
			ITextAwareEditPart source) {
		if (source.getFigure() instanceof WrappingLabel)
			return new TextCellEditorLocator((WrappingLabel) source.getFigure());
		else {
			return new LabelCellEditorLocator((Label) source.getFigure());
		}
	}

	/**
	 * @generated
	 */
	static private class TextCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private WrappingLabel wrapLabel;

		/**
		 * @generated
		 */
		public TextCellEditorLocator(WrappingLabel wrapLabel) {
			this.wrapLabel = wrapLabel;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getWrapLabel() {
			return wrapLabel;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getWrapLabel().getTextBounds().getCopy();
			getWrapLabel().translateToAbsolute(rect);
			if (!text.getFont().isDisposed()) {
				if (getWrapLabel().isTextWrapOn()
						&& getWrapLabel().getText().length() > 0) {
					rect.setSize(new Dimension(text.computeSize(rect.width,
							SWT.DEFAULT)));
				} else {
					int avr = FigureUtilities.getFontMetrics(text.getFont())
							.getAverageCharWidth();
					rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
							SWT.DEFAULT)).expand(avr * 2, 0));
				}
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}

	/**
	 * @generated
	 */
	private static class LabelCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private Label label;

		/**
		 * @generated
		 */
		public LabelCellEditorLocator(Label label) {
			this.label = label;
		}

		/**
		 * @generated
		 */
		public Label getLabel() {
			return label;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getLabel().getTextBounds().getCopy();
			getLabel().translateToAbsolute(rect);
			if (!text.getFont().isDisposed()) {
				int avr = FigureUtilities.getFontMetrics(text.getFont())
						.getAverageCharWidth();
				rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
						SWT.DEFAULT)).expand(avr * 2, 0));
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}
}
